<template>
    <div class="app-container">
        <el-form :model="queryParams"
                 ref="queryForm"
                 :inline="true"
                 v-show="showSearch"
                 label-width="80px"
        >
            <el-form-item label="模板名称" prop="name">
                <el-input
                    v-model="queryParams.name"
                    placeholder="请输入模板名称"
                    clearable
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="模板编号" prop="code">
                <el-input
                    v-model="queryParams.code"
                    placeholder="请输入模版编码"
                    clearable
                    @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="请选择开启状态"
                    clearable
                >
                    <el-option
                        v-for="item in dict.type.sys_notice_status"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="创建时间" prop="createTime">
                <el-date-picker
                    v-model="queryParams.createTime"
                    value-format="yyyy-MM-dd"
                    format="yyyy 年 MM 月 dd 日"
                    type="daterange"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :default-time="['00:00:00', '23:59:59']"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                >新增
                </el-button>
            </el-col>
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--          size="mini"-->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['business:flowCard:export']"-->
            <!--        >导出</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="notifyTemplateList">
            <el-table-column
                label="模板编码"
                align="center"
                prop="code"
                width="120"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="模板名称"
                align="center"
                prop="name"
                width="120"
                :show-overflow-tooltip="true"
            />
            <el-table-column label="类型" align="center" prop="type">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.system_notify_template_type" :value="scope.row.type"/>
                </template>
            </el-table-column>
            <el-table-column label="发送人名称" align="center" prop="nickname"/>
            <el-table-column
                label="模板内容"
                align="center"
                prop="content"
                width="200"
                :show-overflow-tooltip="true"
            />
            <el-table-column label="开启状态" align="center" prop="status" width="80">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_notice_status" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleUpdate(scope.row)"
                    >修改
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleSendForm(scope.row)"
                    >测试
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleDelete(scope.row)"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改流量卡信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
                <el-form-item label="模版编码" prop="code">
                    <el-input v-model="form.code" placeholder="请输入模版编码"/>
                </el-form-item>
                <el-form-item label="模板名称" prop="name">
                    <el-input v-model="form.name" placeholder="请输入模版名称"/>
                </el-form-item>
                <el-form-item label="发件人名称" prop="nickname">
                    <el-input v-model="form.nickname" placeholder="请输入发件人名称"/>
                </el-form-item>
                <el-form-item label="模板内容" prop="content">
                    <el-input type="textarea" v-model="form.content" placeholder="请输入模板内容"/>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-select v-model="form.type" placeholder="请选择类型">
                        <el-option
                            v-for="item in dict.type.system_notify_template_type"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="开启状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio
                            v-for="item in dict.type.sys_notice_status"
                            :key="item.value"
                            :label="item.value"
                        >
                            {{ item.label }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
                <el-button size="small" @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <NotifyTemplateSendForm ref="sendFormRef" @success="getList"></NotifyTemplateSendForm>
    </div>
</template>

<script>

import {
    createNotifyTemplate,
    getNotifyTemplate,
    getNotifyTemplatePage,
    updateNotifyTemplate,
    deleteNotifyTemplate
} from '@/api/system/notity/template'
import NotifyTemplateSendForm from './dialog/NotifyTemplateSendForm.vue'
export default {
    name: 'template',
    dicts: ['sys_notice_status', 'system_notify_template_type'],
    components:{
        NotifyTemplateSendForm
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 信息表格数据
            notifyTemplateList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNo: 1,
                pageSize: 10,
                name: undefined,
                status: undefined,
                code: undefined,
                createTime: []
            },
            // 表单参数
            form: {
                id: null,
                name: '',
                nickname: '',
                code: '',
                content: '',
                type: '',
                params: '',
                status: '0',
                remark: ''
            },
            // 表单校验
            rules: {
                type: [
                    { required: true, message: "消息类型不能为空", trigger: "blur" }
                ],
                status: [
                    { required: true, message: "开启状态不能为空", trigger: "blur" }
                ],
                code: [
                    { required: true, message: "模板编码不能为空", trigger: "blur" }
                ],
                name: [
                    { required: true, message: "模板名称不能为空", trigger: "blur" }
                ],
                nickname: [
                    { required: true, message: "发件人姓名不能为空", trigger: "blur" }
                ],
                content: [
                    { required: true, message: "模板内容不能为空", trigger: "blur" }
                ],
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {

        getList() {
            this.loading = true
            getNotifyTemplatePage(this.queryParams).then(response => {
                this.notifyTemplateList = response.data.list
                this.total = response.data.total
                this.loading = false
            })
        },

        cancel() {
            this.open = false
            this.reset()
        },

        reset() {
            this.form = {
                id: null,
                name: '',
                nickname: '',
                code: '',
                content: '',
                type: null,
                params: '',
                status: '0',
                remark: ''
            }
            this.resetForm('form')
        },

        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },


        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加消息模板信息'
        },

        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getNotifyTemplate(id).then(response => {
                this.form = response.data
                if(this.form.type !==null){
                    this.form.type = this.form.type + ''
                }
                this.form.status = this.form.status + ''
                this.form.electricMeterMode = this.form.electricMeterMode + ''
                this.open = true
                this.title = '修改消息模板信息'
            })
        },
        /** 测试发送操作 */
        handleSendForm(row) {
            this.$refs.sendFormRef.open(row)
        },
        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateNotifyTemplate(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        createNotifyTemplate(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },

        handleDelete(row) {
            this.$modal.confirm('是否确认删除这条数据').then(function () {
                return deleteNotifyTemplate(row.id)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess('删除成功')
            }).catch(() => {
            })
        },

        handleExport() {
            this.download('system/card/export', {
                ...this.queryParams
            }, `card_${new Date().getTime()}.xlsx`)
        }
    }
}
</script>
